package com.bjqh.edu.UI.Activity.Index;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.baijia.player.playback.LivePlaybackSDK;
import com.baijia.player.playback.PBRoom;
import com.baijiahulian.livecore.context.LPConstants;
import com.baijiahulian.livecore.context.LPError;
import com.baijiahulian.livecore.context.LiveRoom;
import com.baijiahulian.livecore.launch.LPLaunchListener;
import com.baijiahulian.livecore.ppt.LPPPTFragment;
import com.baijiahulian.player.playerview.BJBottomViewPresenter;
import com.baijiahulian.player.playerview.BJCenterViewPresenter;
import com.baijiahulian.player.playerview.BJTopViewPresenter;
import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Getoken;
import com.bjqh.edu.Bean.Grade;
import com.bjqh.edu.Bean.Room;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Fragment.MessageListFragment;
import com.bjqh.edu.UI.Fragment.UserListFragment;
import com.bjqh.edu.adapter.PendingAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class VideoActivity extends AppCompatActivity {
    @BindView(R.id.playerView)
    com.baijiahulian.player.BJPlayerView playerView;
    PBRoom mRoom;
    LPPPTFragment mPPTFragment;
    Long rid;
    String token;
    private UserListFragment mUserListFragment;
    private MessageListFragment mMessageListFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_video);
        ButterKnife.bind(this);

        rid = getIntent().getExtras().getLong("rid");
        playerView.setTopPresenter(new BJTopViewPresenter(playerView.getTopView()));
        playerView.setCenterPresenter(new BJCenterViewPresenter(playerView.getCenterView()));
        playerView.setBottomPresenter(new BJBottomViewPresenter(playerView.getBottomView()));
        OkGo.post(AppConstants.BASE_URL + "/rtu/getPayToken.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("id", rid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.i("房间token", s);
                        Gson gson = new Gson();
                        Getoken getoken = gson.fromJson(s, Getoken.class);
                        if (getoken.getMsg().equals("success")) {
                            token = getoken.getToken();
                            mRoom = LivePlaybackSDK.newPlayBackRoom(VideoActivity.this, rid, token, LPConstants.LPDeployType.Product);
                            mRoom.bindPlayerView(playerView);


                            mUserListFragment = new UserListFragment();
                            mUserListFragment.setRoom(mRoom);
                            mMessageListFragment = new MessageListFragment();
                            mMessageListFragment.setRoom(mRoom);
                            mPPTFragment = new LPPPTFragment();
                            mPPTFragment.setLiveRoom(mRoom);
                            mPPTFragment.setFlingEnable(false);
                            getSupportFragmentManager().beginTransaction().add(R.id.big_container, mPPTFragment).commit();
                            getSupportFragmentManager().executePendingTransactions();

                            mRoom.enterRoom(new LPLaunchListener() {

                                @Override
                                public void onLaunchSteps(int i, int i1) {
                                    //  Toast.makeText(VideoActivity.this,i+""+i1,Toast.LENGTH_SHORT).show();

                                }

                                @Override
                                public void onLaunchError(LPError lpError) {
                                    Toast.makeText(VideoActivity.this, "进入回放房间失败" + lpError.getMessage(), Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onLaunchSuccess(LiveRoom liveRoom) {
                                    // Toast.makeText(VideoActivity.this,"进入回放房间成功",Toast.LENGTH_SHORT).show();

//                                    playerView.playVideo();


                                }
                            });


                        } else {
                            Toast.makeText(VideoActivity.this, "小主，该课程暂时没有视频哦~~", Toast.LENGTH_SHORT).show();
                            finish();
                        }


                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(VideoActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                    }
                });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (playerView != null) {
            playerView.onDestroy();
        }


    }
}
